/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onpu_dms_xls.kernel.common.pdf.composite;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import onpu_dms_xls.exceptions.MyException;
import onpu_dms_xls.kernel.common.logAndDebug.LoggingFacade;
import org.apache.log4j.Logger;

/**
 *
 * @author snbm
 */
public class PDF_ExtractWriter extends ExtractWriter implements VariableValueForLabelGetter{
    

    protected List<PDF_ExtractWriterTask> tasks;

    public PDF_ExtractWriter(String fileName, VariableValueForLabelGetter d) {
        super(fileName, d);
        tasks = new ArrayList<PDF_ExtractWriterTask>();
    }

    @Override
    public void print() throws Exception {
        LoggingFacade.trace.debug("call");
        File res = null;
        res = new File(fileName);
        if (res.exists()) {
            res.delete();
            LoggingFacade.trace.info("makeFile deleted existing file, name="+fileName);
        }
        LoggingFacade.trace.debug("makeFile working , tasks size="+tasks.size());
        try {
            if (!res.createNewFile()) {
                throw new MyException("PDF_Vipiska_Creator", "makeFile", "cannot create new .pdf");
            }
        } catch (Exception e) {
            LoggingFacade.trace.error("",e);
        }

        // step 1
        Document document = new Document(PageSize.A4.rotate());
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        // step 3
        document.open();
        // step 4
        document.addTitle("Виписка до диплому");

        PdfContentByte pcb = writer.getDirectContentUnder();

        //перша сторінка
        pcb.saveState();
        for (PDF_ExtractWriterTask pewt : tasks) {
            if (pewt.getPageNum() == 1) {
                String text = pewt.getText();
                LoggingFacade.trace.debug("makeFile printing with data: " + text);
                pcb.setFontAndSize(BaseFont.createFont(pewt.getFont(), pewt.getCodePage(), true), pewt.getFontSize());
                pcb.beginText();
                pcb.showTextAligned(PdfContentByte.ALIGN_LEFT, text, pewt.getX(), pewt.getY(), 0);
                pcb.endText();
            }
        }
        pcb.restoreState();

        //друга сторінка
        document.newPage();
        pcb = writer.getDirectContentUnder();
        pcb.saveState();
        for (PDF_ExtractWriterTask pewt : tasks) {
            if (pewt.getPageNum() == 2) {
                String text = pewt.getText();
                LoggingFacade.trace.debug("makeFile printing with data: " + text);
                pcb.setFontAndSize(BaseFont.createFont(pewt.getFont(), pewt.getCodePage(), true), pewt.getFontSize());
                pcb.beginText();
                pcb.showTextAligned(PdfContentByte.ALIGN_LEFT, text, pewt.getX(), pewt.getY(), 0);
                pcb.endText();
            }
        }
        pcb.restoreState();

        // step 5
        document.close();
        LoggingFacade.trace.debug("document.close();");
    }



    @Override
    public void addTask(ExtractWriterTask task) {
        if (!(task instanceof PDF_ExtractWriterTask)) {
            throw new IllegalArgumentException("only PDF_ExtractWriterTask needed");
        }
        addTask((PDF_ExtractWriterTask) task);
    }

    private void addTask(PDF_ExtractWriterTask t) {
        tasks.add(t);
    }

    @Override
    public void removeTask(ExtractWriterTask task) {
        if (!(task instanceof PDF_ExtractWriterTask)) {
            throw new IllegalArgumentException("only PDF_ExtractWriterTask needed");
        }
        removeTask((PDF_ExtractWriterTask) task);
    }

    private void removeTask(PDF_ExtractWriterTask t) {
        tasks.remove(t);
    }

    @Override
    public String getVariableValueForLabel(ExtractLabel label) {
        return dataToPrintSource.getVariableValueForLabel(label);
    }
}
